<?php

namespace app\merchant\model;

use app\common\model\Comment as CommentModel;

/**
 * 商品评价模型
 * Class Comment
 * @package app\store\model
 */
class Comment extends CommentModel
{

    /**
     * 关联商品图片
     * @return \think\model\relation\BelongsTo
     */
    public function goodsImage()
    {

        $model = "app\\common\\model\\UploadFile";
        return $this->belongsTo($model, 'image_id', 'file_id')->field('file_id,storage,file_url,file_name');
    }

    /**
     * 获取评价列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getMerchantList()
    {

        return $this->with(['goodsImage'])
            ->alias('a')
            ->join('user b','a.user_id = b.user_id')
            ->join('order_goods c','a.order_goods_id = c.order_goods_id')
            ->field('a.comment_id,a.score,a.content,a.sort,a.status,a.order_id,a.create_time,b.nickName,b.user_id,c.goods_name,c.goods_id,c.image_id')
            ->where('a.is_delete', '=', 0)
            ->order(['a.sort' => 'asc', 'a.create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

    /**
     * 评价详情
     * @param $comment_id
     * @return Comment|null
     * @throws \think\exception\DbException
     */
    public static function merchantDetail($comment_id)
    {
        $list = self::with(['goodsImage','image.file'])
            ->alias('a')
            ->join('user b','a.user_id = b.user_id')
            ->join('order_goods c','a.order_goods_id = c.order_goods_id')
            ->field('a.comment_id,a.score,a.content,a.sort,a.status,a.order_id,a.create_time,a.comment_id,b.nickName,b.user_id,c.goods_name,c.goods_id,c.image_id')
            ->where('a.comment_id', '=', $comment_id)
            ->find();
        return $list;

    }


    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 状态修改
     * @return false|int
     */
    public function setState($status)
    {
        return $this->save(['status' => $status]);
    }


    /**
     * 获取评价总数量
     * @return int|string
     */
    public function getCommentTotal()
    {
        return $this->where(['is_delete' => 0])->count();
    }

}